/**
 * 
 */
package cs5226.model;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @author Sunny
 *
 */
public class SQLResultSet {
	
	private ArrayList<String> headerList = new ArrayList<String>();
	private ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
	
	public ArrayList<String> getHeader()
	{
		return headerList;
	}
	
	public ArrayList<ArrayList<String>> getData()
	{
		return data;
	}
	
	public void populate(ResultSet rs)
	{		
		headerList.clear();
		data.clear();
		
		try {
			//get headers
			ResultSetMetaData metadata = rs.getMetaData();
			int columnCount = metadata.getColumnCount();
			for (int i = 0; i < columnCount; i++) {
				String columnName = metadata.getColumnName(i + 1);
				headerList.add(columnName);
			}
			
			//get data
			while (rs.next())
			{
				ArrayList<String> row = new ArrayList<String>();
				for (int i = 0; i < columnCount; i++) {
					String val = rs.getString(i+1);
					row.add(val);
				}
				data.add(row);
			}			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
